// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;


const int N = 3e5 + 50;

int max1[N], max2[N], dpf[N], son[N];
vector<int> G[N];


inline void update(int &a, int &b, int x) {
    if (x > a) {
        b = a;
        a = x;
    } else if (x > b)
        b = x;
}

int n;

void dfs1(int u, int fa) {
    // dbg(u, fa, G[u].size());
    if (G[u].size() == 1 && fa != u) {
        // dbg(u);
        // dps[u] = 1;
        max1[u] = son[u] = 1;
        return;
    }
    for (int v : G[u]) {
        if (v == fa)
            continue;
        dfs1(v, u);
        update(max1[u], max2[u], son[v]);
    }
    son[u] = max1[u] + G[u].size() - (max1[u] != 0);
}

void dfs2(int u, int fa) {
    if (u == fa) {
        dpf[u] = max1[u] + max2[u] + G[u].size() - (max1[u] != 0) - (max2[u] != 0) + 1;
    } else {
        int vfa = max1[fa];
        if (vfa == son[u])
            vfa = max2[fa];
        vfa = vfa + G[fa].size() - (vfa != 0);

        update(max1[u], max2[u], vfa);
        dpf[u] = max1[u] + max2[u] + G[u].size() - (max1[u] != 0) - (max2[u] != 0) + 1;
    }
    for (int v : G[u]) {
        if (v == fa)
            continue;
        dfs2(v, u);
    }
}


int main(int argc, char const *argv[]) {
    // code

    int t;
    scanf("%d", &t);
    for (int kk=0; kk<t; ++kk) {
        scanf("%d", &n);
        memset(max1, 0, 4 * (n + 1));
        memset(max2, 0, 4 * (n + 1));

        for (int i=0; i<=n; ++i)
            G[i].clear();

        for (int i=0; i<n-1; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }

        dfs1(1, 1);

        for (int i=1; i<=n; ++i)
            dbg(i, max1[i], max2[i], dpf[i], G[i].size(), son[i]);
        dfs2(1, 1);

        int ans = 0;
        for (int i=1; i<=n; ++i)
            ans = max(ans, dpf[i]);
        printf("%d\n", ans);

        for (int i=1; i<=n; ++i)
            dbg(i, max1[i], max2[i], dpf[i], G[i].size());
    }

    return 0;
}
